//
//  HomePageViewController.m
//  Relax
//
//  Created by yanghuiyan on 16/4/18.
//  Copyright © 2016年 yanghuiyan. All rights reserved.
//

#import "HomePageViewController.h"
#import "HomePageDataModel.h"
#import "HomePageBeforeVC.h"
#import "YHY_Search_ViewController.h"
@interface HomePageViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, retain) YHYBaseCollectionView *homeCollectionView;

@property (nonatomic, retain) NSMutableArray *dataSource;

@end

@implementation HomePageViewController

- (void)dealloc {
    
    _homeCollectionView.delegate = nil;
    _homeCollectionView.dataSource = nil;
    
    [_dataSource release];
    [_homeCollectionView release];
    
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.dataSource = [NSMutableArray array];
    [self dataHandle];
    [self creatView];
    
    /**< 接收收藏通知 添加观察者 */
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(favoriteAction) name:@"favorite" object:nil];
}


- (void)creatView {
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.edgesForExtendedLayout=UIRectEdgeNone;
    
    //导航栏左侧按钮
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"搜索"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ] style:UIBarButtonItemStylePlain target:self action:@selector(leftItemAction:)];
    self.navigationItem.leftBarButtonItem = leftItem;
    [leftItem release];

    
    //自定义标题视图
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 40)];
    titleLabel.text = @"首 页";
    titleLabel.font = [UIFont boldSystemFontOfSize:22];
    titleLabel.textColor = [UIColor grayColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView = titleLabel;
    [titleLabel release];

    
    // 创建collectionView
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(WIDTH, HEIGHT);
    flowLayout.minimumLineSpacing = 0;
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    self.homeCollectionView = [[YHYBaseCollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) collectionViewLayout:flowLayout];
    [_homeCollectionView registerClass:[HomePageCollectionViewCell class] forCellWithReuseIdentifier:@"home"];
    _homeCollectionView.backgroundColor = [UIColor whiteColor];
//    _homeCollectionView.bounces = NO;
    _homeCollectionView.pagingEnabled = YES;
    _homeCollectionView.delegate = self;
    _homeCollectionView.dataSource = self;
    
    // 设置整页
    _homeCollectionView.pagingEnabled = YES;
    
    // 关闭回弹
//    _homeCollectionView.bounces = NO;
    
    
    [self.view addSubview:_homeCollectionView];
    
    [_homeCollectionView release];
    
}

- (void)leftItemAction:(UIBarButtonItem *)sender {
    
    YHY_Search_ViewController *searchVC = [[YHY_Search_ViewController alloc] init];
    [self.navigationController pushViewController:searchVC animated:YES];
    
}

/**
 *  实现collectionView协议方法
 */

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    
       if (_dataSource.count != 0) {
        
        return self.dataSource.count;
        
    }

    return 0;
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    HomePageCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"home" forIndexPath:indexPath];
        
    HomePageDataModel *model = self.dataSource[indexPath.row];
    
//    cell.delegate = self;
    
    cell.dataModel = model;
        
    return cell;
   
}


/**
 *  监测偏移量
 */

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    if (_homeCollectionView.contentOffset.x > _homeCollectionView.bounds.size.width * (self.dataSource.count - 1)) {
     
        HomePageBeforeVC *beforeVC = [[HomePageBeforeVC alloc] init];
        
        HomePageDataModel *model = self.dataSource[0];
        
        beforeVC.month = [NSMutableString stringWithString:model.hp_makettime];
        
        [self.navigationController pushViewController:beforeVC animated:YES];
    
    }
    
}

#pragma mark - 网络请求

- (void)dataHandle {
    
    [YHYAFNetTool GETNetWirhUrl:@"http://v3.wufazhuce.com:8000/api/hp/more/0" body:nil headerFile:nil response:YHYJSON Success:^(id result) {
        
        for (NSDictionary *dic in result[@"data"]) {
            
            HomePageDataModel *dataModel = [[HomePageDataModel alloc] initWithDataSource:dic];
            
            [self.dataSource addObject:dataModel];
            
        }
        
        [self.homeCollectionView reloadData];
        
    } Failure:^(NSError *error) {
        
    }];
    
}

#pragma mark - ------------ 收藏 ---------------
- (void)favoriteAction {
    
    [self.homeCollectionView reloadData];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
